import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class EchoTCP1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ServerSocket serverSock = new ServerSocket(30000);
			while(true){
					Socket sock = serverSock.accept();
					System.out.println(sock.getInetAddress().getHostAddress());
					InputStream is = sock.getInputStream();
					OutputStream os = sock.getOutputStream();
					int i = is.read(); 
					while(i != -1){
						if(i != '\n'){
							System.out.print((char) i);
						}else{
							os.write("Message recieved\n".getBytes());
							os.flush();	
						}
						i = is.read();
					}
					sock.close();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
